package com.lhkj.ct.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.config.FileServerConfig;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.OperatorType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.base.utils.file.FileUploadUtils;
import com.lhkj.ct.base.utils.file.MimeTypeUtils;
import com.lhkj.ct.meta.modules.common.enums.AnswerState;
import com.lhkj.ct.meta.modules.patient.model.dto.PatBasicInfo;
import com.lhkj.ct.meta.modules.patient.service.PatientService;
import com.lhkj.ct.meta.modules.psychology.model.vo.PsyRecordsVo;
import com.lhkj.ct.meta.modules.psychology.service.PsyRecordService;
import com.lhkj.ct.meta.modules.questionnaire.model.entity.TblQnrAllotRecord;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAllotRecordService;
import com.lhkj.ct.platform.service.MyPersonalCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/my/personal/center")
@Api(tags = {"5.用户中心"})
public class MyPersonalCenterController {

    @Resource
    private MyPersonalCenterService personalCenterService;

    @Resource
    private PsyRecordService psyRecordService;

    @Resource
    private QnrAllotRecordService qnrAllotRecordService;

    @Resource
    private PatientService patientService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "统计未完成", notes = "psy:未完成测验数量 qnr:未完成问卷数量")
    @GetMapping("count")
    public ActionResult<Map<String, Long>> countUndone() {
        return ActionResult.success(personalCenterService.count());
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测验列表")
    @GetMapping("undonePsy")
    public ActionResult<IPage<PsyRecordsVo>> selectPatUnitPage() {
        PsyRecordsVo psyRecordsVo = new PsyRecordsVo();
        psyRecordsVo.setStateList(new AnswerState[]{AnswerState.UNDONE, AnswerState.DOING});
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPage(1);
        pageQuery.setLimit(5);
        return ActionResult.success(psyRecordService.selectPagesByPatient(psyRecordsVo, pageQuery));
    }


    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "问卷列表")
    @GetMapping("undoneQnr")
    public ActionResult<IPage<TblQnrAllotRecord>> selectPatUnitPage(TblQnrAllotRecord record, PageQuery pageQuery) {
        record.setPatId(ShiroUtil.getAuthUserInfo().getUserId());
        record.setStatus(9);
        pageQuery.setPage(1);
        pageQuery.setLimit(5);
        return ActionResult.success(qnrAllotRecordService.selectAllotPage(record, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "个人信息")
    @GetMapping("profile")
    public ActionResult<PatBasicInfo> profile() {
        return ActionResult.success(patientService.getBasicInfo());
    }

    /**
     * 头像上传
     */
    @ApiOperation("上传头像")
    @ApiOperationSupport(order = 3)
    @Log(title = "用户头像", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    @PreventDuplicateSubmit
    @PostMapping("/avatar")
    public ActionResult<?> avatar(@RequestParam("avatar") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(FileServerConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (patientService.updateUserAvatar(ShiroUtil.getAuthUserInfo().getUserId(), avatar)) {
                Map<String, String> map = new HashMap<>();
                map.put("imgUrl", avatar);
                return ActionResult.success(map);
            }
        }
        return ActionResult.fail("上传图片异常，请联系管理员");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "更新个人信息")
    @PutMapping("updateBasicInfo")
    @PreventDuplicateSubmit
    @Log(title = "个人中心", actionType = ActionType.UPDATE, operatorType = OperatorType.USER)
    public ActionResult<Void> profile(@Validated @RequestBody PatBasicInfo info) {
        return ActionResult.judge(patientService.updateBasicInfo(info));
    }
}
